package com.xoa.dev.workFlow.service;

import com.xoa.dao.WFE.WFEFormTypeMapper;
import com.xoa.dao.users.UserPrivMapper;
import com.xoa.model.department.Department;
import com.xoa.model.users.UserPriv;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowFormType;
import com.xoa.model.workflow.FlowProcess;
import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.service.WFE.WFEFactory.WFEFlowProcess;
import com.xoa.service.WFE.WFERunner.WFEFlowRunInfo;
import com.xoa.service.WFE.WFERunner.WFEFlowRunPrcs;
import com.xoa.service.department.DepartmentService;
import com.xoa.util.AjaxJson;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class WorkFlowSelectBranchService {

    @Autowired
    WFEFlowRunPrcs wfeFlowRunPrcs;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    WFEFlowProcess wfeFlowProcess;

    @Autowired
    WFEFlowRunInfo wfeFlowRunInfo;

    @Autowired
    WFEFormTypeMapper wfeFormTypeMapper;

    @Autowired
    UserPrivMapper userPrivMapper;


    public AjaxJson getBranch(Integer flowId, Integer currentFlowPrcs, Integer runId, Users users) {
        AjaxJson ajaxJson = new AjaxJson();
        //定义流程下一步骤获取的所有对象
        List<Map<String, Object>> reMap = new ArrayList<Map<String, Object>>();
        //定义获取步骤对应流程信息
        Map<String, Object> temp = null;
        //用来传参取数据
        Map<String, Object> maprunId = new HashMap<String, Object>();
        try {
            maprunId.put("runId", runId);
            //根据流水号获取流程信息
            List<FlowRunPrcs> allFrp = wfeFlowRunPrcs.findByRunId(maprunId);
            Department dep = departmentService.getDeptById(users.getDeptId());
            //获取所有流程步骤
            Map<String, FlowProcess> map = wfeFlowRunInfo.allflowProcessBymap(flowId);
            //获取当前流程
            FlowProcess flowProcess = wfeFlowProcess.findbyprcsId(flowId, currentFlowPrcs);
            temp = new HashMap<String, Object>();
            if (!StringUtils.checkNull(flowProcess.getPrcsOutSet())){
                temp.put("prcsOut", flowProcess.getPrcsOut());
                temp.put("prcsOutSet", flowProcess.getPrcsOutSet());
                temp.put("conditionDesc", flowProcess.getConditionDesc());
            }
            List<FlowFormType> forms = wfeFormTypeMapper.getFrombyFlowId(flowId);
            FlowFormType formType = forms.get(0);
            temp.put("html",formType.getPrintModel());
            temp.put("syncDeal", flowProcess.getSyncDeal());
            temp.put("SYS_USERPRIV", users.getUserPrivName());
            temp.put("SYS_DEPTNAME_SHORT", users.getDeptName());
            if (!StringUtils.checkNull(users.getUserPrivOther())) {
                StringBuffer stringBuffer = new StringBuffer();
                String[] strings = users.getUserPrivOther().split(",");

                for (int i = 0; i < strings.length; i++) {
                    UserPriv userPriv1 = userPrivMapper.getUserPriv(Integer.parseInt(strings[i]));
                    if (userPriv1 != null) {
                        stringBuffer.append(userPriv1.getPrivName());
                        stringBuffer.append(",");
                    }
                }
                temp.put("SYS_USERPRIVOTHER", stringBuffer.toString());
            }

            ajaxJson.setAttributes(temp);

            FlowProcess nextFp = null;
            String prcsTo = flowProcess.getPrcsTo();
            if (StringUtils.checkNull(prcsTo)) {
                nextFp = map.get(flowProcess.getPrcsId() + 1 + "");
                temp = wfeFlowRunInfo.reTemp(nextFp, dep, users, runId, allFrp);
                reMap.add(temp);
            } else {
                String[] prcsToArray = prcsTo.split(",");
                for (int i = 0, length = prcsToArray.length; i < length; i++) {
                    if (!"0".equals(prcsToArray[i])) {
                        nextFp = map.get(prcsToArray[i]);
                    }else{
                        nextFp=null;
                    }
                        temp = wfeFlowRunInfo.reTemp(nextFp, dep, users, runId, allFrp);
                        reMap.add(temp);

                }
            }
            ajaxJson.setObj(reMap);
            ajaxJson.setFlag(true);
            ajaxJson.setMsg("OK");
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(true);
        }
        return ajaxJson;
    }



}
